package com.amc.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.amc.dao.IFoodDao;
import com.amc.dto.FoodView;
import com.amc.entity.Food;
import com.amc.entity.FoodType;

@Repository
public class FoodDaoImpl implements IFoodDao {
	@PersistenceContext
	private EntityManager em;

	public void setEm(EntityManager em) {
		this.em = em;
	}

	@Override
	public List<FoodView> queryFoodByKeyWord(String keyWord) {
		Query query = em
				.createQuery("select f from Food f join fetch f.foodType where f.foodName like '%"
						+ keyWord + "%'");
		List<Food> foods = new ArrayList<>();
		List<FoodView> fvs = new ArrayList<>();
		try {
			foods = query.getResultList();
			for (Food f : foods) {
				fvs.add(new FoodView(null,f.getFoodId(), f.getFoodName(), f
						.getFoodPrice(), f.getFoodPic(), f.getFoodType()
						.getTypeId(), 1));
			}
		} catch (Exception e) {
			System.out.println("菜搜索失败");
		}
		return fvs;
	}

	@Override
	public List<FoodType> queryFoodTypeByMId(String mId) {
		Query query = em.createQuery(
				"select ft from Food f join  f.foodType ft  where  "
						+ "f.foodMerchant in (select m from Food f join  "
						+ " f.foodMerchant m where m.merchantId=?) ")
				.setParameter(1, mId);
		List<FoodType> foodTypes = new ArrayList<>();
		try {
			foodTypes = query.getResultList();
		} catch (Exception e) {
			System.out.println("商家菜品列表查询失败");
		}
		return foodTypes;
	}

	@Override
	public List<FoodView> queryFoodByFoodType(String mId, String tId) {
		Query query = em
				.createQuery(
						"select f from Food f join  f.foodType ft  where ft.typeId=? and "
								+ "f.foodMerchant.merchantId =? ")
				.setParameter(1, tId).setParameter(2, mId);
		List<Food> foods = new ArrayList<>();
		List<FoodView> fvs = new ArrayList<>();
		try {
			foods = query.getResultList();
			for (Food f : foods) {
				fvs.add(new FoodView(null,f.getFoodId(), f.getFoodName(), f
						.getFoodPrice(), f.getFoodPic(), f.getFoodType()
						.getTypeId(), 1));
			}
		} catch (Exception e) {
			System.out.println("商家菜品列表点击查询菜列表失败");
		}
		return fvs;
	}

	@Override
	public List<FoodView> queryFoodByMId(String mId) {
		Query query = em
				.createQuery(
						"select f from Food f join f.foodMerchant m where m.merchantId=?")
				.setParameter(1, mId);
		List<Food> foods = new ArrayList<>();
		List<FoodView> fvs = new ArrayList<>();
		try {
			foods = query.getResultList();
			for (Food f : foods) {
				fvs.add(new FoodView(null,f.getFoodId(), f.getFoodName(), f
						.getFoodPrice(), f.getFoodPic(), f.getFoodType()
						.getTypeId(), 1));
			}
		} catch (Exception e) {
			System.out.println("商家菜列表查询失败");
		}
		return fvs;
	}

	@Override
	public FoodView queryFood(String foodId) {
		Food food=em.find(Food.class, foodId);
		FoodView foodView=new FoodView(null,food.getFoodId(), food.getFoodName(), food
				.getFoodPrice(), food.getFoodPic(), food.getFoodType()
				.getTypeId(), 1);
		return foodView;
	}

}
